<template>
  <div class="app-container">
    <div class="custom_card_0" v-show="showSearch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="图文名称" prop="graphicName">
          <el-input
            v-model="queryParams.graphicName"
            placeholder="请输入图文名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="图文类型" prop="graphicType">
          <el-select
            v-model="queryParams.graphicType"
            placeholder="请选择图文类型"
            clearable
            size="small"
          >
            <el-option
              v-for="dict in dict.type.graphic_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button
            class="first-plain-btn"
            size="medium"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['system:graphic:add']"
            >新增</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="graphicList" border>
        <el-table-column
          label="名称"
          align="center"
          prop="graphicName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="图片"
          align="center"
          prop="graphicUrl"
          min-width="100"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-image
              v-show="
                scope.row.graphicUrl != null && scope.row.graphicUrl != ''
              "
              :src="emvImageUrl + scope.row.graphicUrl"
              style="width: 60px; height: 30px"
              :preview-src-list="srcList"
              @click="vbs(scope.row.graphicUrl)"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column
          label="图文类型"
          align="center"
          prop="graphicType"
          min-width="100"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.graphic_type"
              :value="scope.row.graphicType"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="所属租户"
          align="center"
          prop="tenantName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="更新时间"
          align="center"
          prop="updateTime"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="操作"
          align="center"
          width="200"
          fixed="left"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:graphic:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleShow(scope.row)"
              v-hasPermi="['system:graphic:query']"
              >详情</el-button
            >
            <el-button
              size="mini"
              type="text"
              v-if="scope.row.graphicType == 14"
              @click="handleDelete(scope.row)"
              handleDelete
              v-hasPermi="['system:graphic:edit']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <el-dialog :title="title" :visible.sync="addOpen" width="800px" append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="图文名称" prop="graphicName">
              <el-input
                v-model="titleName"
                disabled
                placeholder="请输入图文名称"
                class="el-input-style"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24"></el-col>
          <el-col :span="12">
            <el-form-item label="图文类型" prop="graphicType">
              <el-input
                v-model="titleName"
                disabled
                placeholder="请输入图文类型"
                class="el-input-style"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片" prop="graphicUrl">
              <MmvUploadImage
                @upImg="upgraphicUrl"
                ref="graphicUrlAdd"
                :autoCropWidth="width"
                :autoCropHeight="height"
                size="2"
              >
                <el-button size="small" type="primary" icon="el-icon-upload"
                  >添加图片</el-button
                >
                <span slot="tip" class="el-upload__tip">
                  支持JPG/JPEG/PNG，尺寸{{ width }} px *
                  {{ height }} px，不超过2M，仅限1张图片
                </span>
              </MmvUploadImage>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="跳转类型">
              <el-radio-group v-model="form.jumpType">
                <el-radio
                  v-for="dict in dict.type.jump_type"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="24" v-if="form.jumpType==1">
            <el-form-item label="图文介绍" prop="graphicIntroduction">
              <EditorOld v-if="addOpen" v-model="form.graphicIntroduction" :min-height="192"/>
            </el-form-item>
          </el-col>

          <el-col :span="24" v-if="form.jumpType==2">
            <el-form-item label="小程序Appid" prop="appid">
              <el-input v-model="form.appid" placeholder="小程序Appid" class="el-input-style"/>
            </el-form-item>
          </el-col>

          <el-col :span="24" v-if="form.jumpType==2||form.jumpType==3">
            <el-form-item label="页面路径" prop="path">
              <el-input v-model="form.path" placeholder="页面路径" class="el-input-style"/>
            </el-form-item>
          </el-col>

          <el-col :span="24" v-if="form.jumpType==4">
            <el-form-item label="文章链接" prop="path">
              <el-input v-model="form.path" placeholder="文章链接" class="el-input-style"/>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="addCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改H5图文对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="图文名称" prop="graphicName">
              <el-input v-model="form.graphicName" disabled class="el-input-style"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24"></el-col>
          <el-col :span="12">
            <el-form-item label="图文类型" prop="graphicType">
              <el-select
                v-model="form.graphicType"
                placeholder="请选择图文类型"
                clearable
                disabled
                style="width: 100%"
                size="small"
              >
                <el-option
                  v-for="dict in dict.type.graphic_type"
                  :key="parseInt(dict.value)"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片" prop="graphicUrl">
              <MmvUploadImage
                @upImg="upgraphicUrl"
                ref="graphicUrlEdit"
                :autoCropWidth="width"
                :autoCropHeight="height"
                size="2"
              >
                <el-button size="small" type="primary" icon="el-icon-upload"
                  >添加图片</el-button
                >
                <span slot="tip" class="el-upload__tip">
                  支持JPG/JPEG/PNG，尺寸{{ width }} px *
                  {{ height }} px，不超过2M，仅限1张图片
                </span>
              </MmvUploadImage>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.graphicType==14">
            <el-form-item label="跳转类型">
              <el-radio-group v-model="form.jumpType">
                <el-radio
                  v-for="dict in dict.type.jump_type"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.jumpType==1">
            <el-form-item label="图文介绍" prop="graphicIntroduction">
              <EditorOld v-if="open" v-model="form.graphicIntroduction" :min-height="192"/>
            </el-form-item>
          </el-col>

          <el-col :span="24" v-if="form.jumpType==2">
            <el-form-item label="小程序Appid" prop="appid">
              <el-input v-model="form.appid" placeholder="小程序Appid" class="el-input-style"/>
            </el-form-item>
          </el-col>

          <el-col :span="24" v-if="form.jumpType==2||form.jumpType==3">
            <el-form-item label="页面路径" prop="path">
              <el-input v-model="form.path" placeholder="页面路径" class="el-input-style"/>
            </el-form-item>
          </el-col>

          <el-col :span="24" v-if="form.jumpType==4">
            <el-form-item label="文章链接" prop="path">
              <el-input v-model="form.path" placeholder="文章链接" class="el-input-style"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情 -->
    <el-dialog
      :title="title"
      :visible.sync="openShow"
      width="800px"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="图文名称" prop="graphicName" style="width: 50%">
          {{ form.graphicName }}
        </el-form-item>
        <el-form-item label="图文类型" prop="graphicType" style="width: 50%">
          <dict-tag
            :options="dict.type.graphic_type"
            :value="form.graphicType"
          />
        </el-form-item>
        <el-form-item label="图片">
          <el-image
            v-show="form.graphicUrl != null && form.graphicUrl != ''"
            :preview-src-list="srcList"
            :src="emvImageUrl + form.graphicUrl"
            style="width: 160px; height: 90px; margin-right: 15px"
            fit="contain"
            @click="vbs(form.graphicUrl)"
          >
          </el-image>
        </el-form-item>
          <el-form-item label="小程序Appid" prop="appid"  v-if="form.jumpType==2">
            {{ form.appid }}
          </el-form-item>
          <el-form-item label="页面路径" prop="path"  v-if="form.jumpType==2||form.jumpType==3">
            {{ form.path }}
          </el-form-item>
          <el-form-item label="文章链接" prop="path" v-if="form.jumpType==4">
            {{ form.path }}
          </el-form-item>
          <el-form-item label="图文介绍" v-if="form.jumpType==1">
            <EditorOld v-if="openShow" v-model="form.graphicIntroduction" :min-height="192" :readOnly="disabled"/>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelShow">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGraphic,
  getGraphic,
  delGraphic,
  addGraphic,
  updateGraphic,
  exportGraphic,
} from '@/api/system/graphic'
import MmvUploadImage from './mmv-upload-image.vue'
import { delUser } from '@/api/system/user'
import EditorOld from "@/components/Editor/indexOld";
export default {
  components: {
    MmvUploadImage,
    EditorOld
  },
  name: 'Graphic',
  dicts: ['graphic_type','jump_type'],
  data() {
    return {
      titleName: '首页轮播',
      disabled: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      addOpen: false,
      // 总条数
      total: 0,
      emvImageUrl: process.env.VUE_APP_BASE_IMAGE_URL,
      // H5图文表格数据
      graphicList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openShow: false,
      srcList: [],
      width: 200,
      height: 200,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        graphicName: null,
        graphicType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        graphicIntroduction: [
          {
            required: true,
            message: '请输入图文介绍',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    vbs(val) {
      console.log(val)
      this.srcList = []
      this.srcList.push(this.getImgUrl(val))
    },
    graphicTypeInfo(type) {
      switch (type) {
        case 1:
          this.width = 340
          this.height = 240
          break
        case 2:
          this.width = 334
          this.height = 118
          break
        case 3: //平台保障4商务合作5微信分介绍6用车服务协议7预定须知8开票帮助9违章处理说明10关于我们
          this.width = 334
          this.height = 118
          break
        case 14:
          this.width = 750
          this.height = 336
          break
      }
    },
    upgraphicUrl(url) {
      //图片上传成功地址
      this.form.graphicUrl = url[0]
      this.$refs['form'].validateField('graphicUrl')
    },
    /** 查询H5图文列表 */
    getList() {
      this.loading = true
      listGraphic(this.queryParams).then((response) => {
        this.graphicList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    addCancel() {
      this.addOpen = false
      this.reset()
    },
    cancelShow() {
      this.openShow = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        graphicId: null,
        graphicName: null,
        graphicUrl: null,
        graphicType: null,
        graphicIntroduction: null,
        tenantId: null,
        delFlag: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        remark: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form.graphicUrl = ''
      this.$nextTick(() => {
        this.$refs.graphicUrlAdd.previewToUrl(this.form.graphicUrl)
      })
      this.reset()
      this.addOpen = true
      this.title = '新增首页轮播'
      this.width = 750
      this.height = 336
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const graphicId = row.graphicId || this.ids
      getGraphic(graphicId).then((response) => {
        this.form = response.data
        this.graphicTypeInfo(this.form.graphicType)
        this.$nextTick(() => {
          this.$refs.graphicUrlEdit.previewToUrl(this.form.graphicUrl)
        })
        this.open = true
        this.title = '修改H5图文'
      })
    },
    /* 详情 */
    handleShow(row) {
      this.reset()
      const graphicId = row.graphicId || this.ids
      getGraphic(graphicId).then((response) => {
        this.form = response.data
        this.openShow = true
        this.title = '详情'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.remark = encodeURI(this.form.graphicIntroduction)
          if (this.form.graphicId != null) {
            updateGraphic(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            this.form.graphicName = '首页轮播'
            this.form.graphicType = 14
            addGraphic(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.addOpen = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.graphicId || this.ids
      this.$modal
        .confirm('是否确认删除数据项？')
        .then(function () {
          return delGraphic(id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
  },
}
</script>
<style scoped>
.el-upload__tip {
  margin-left: 8px;
}
::v-deep .preview-wrap {
  margin: 12px 0 0 !important;
}
.img {
  height: 300px;
  border: 1px solid #ccc;
  display: block;
  border-radius: 5px;
  margin-bottom: 15px;
}
</style>
